import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class NEWIrc extends Frame {
	private static final long serialVersionUID = 1L;
	public TextArea		text;
	public TextField	data;
	public Label		count;
	public NEWSentence_itf	sentence;
	static String		myName;

	public static void main(String argv[]) {
		
		if (argv.length != 1) {
			System.out.println("java Irc <name>");
			return;
		}
		myName = argv[0];
	
		// initialize the system
		Client.init();
		
		
		// look up the IRC object in the name server
		// if not found, create it, and register it in the name server
		NEWSentence_itf s = (NEWSentence_itf)Client.lookup("NEWIRC");
		if (s == null) {
			s = (NEWSentence_itf)Client.create(new NEWSentence());
			Client.register("NEWIRC", s);
		}
		
		// create the graphical part
		new NEWIrc(s);
		

	}

	public NEWIrc(NEWSentence_itf s) {
		sentence = s;
		setLayout(new FlowLayout());
	
		text=new TextArea(10,60);
		text.setEditable(false);
		text.setForeground(Color.red);
		add(text);
	
		data=new TextField(60);
		add(data);
	
		Button write_button = new Button("write");
		write_button.addActionListener(new NEWwriteListener(this));
		add(write_button);
		Button read_button = new Button("read");
		read_button.addActionListener(new NEWreadListener(this));
		add(read_button);
		
		sentence.lock_read();
		count = new Label(sentence.getNbWritings()+" messages written");
		sentence.unlock();
		add(count);
		
		setSize(470,300);
		text.setBackground(Color.black); 
		
		addWindowListener (new WindowAdapter(){
			public void windowClosing (WindowEvent e){
				System.exit(0);
			}
		});

		
		show();
	}
}



class NEWreadListener implements ActionListener {
	NEWIrc irc;
	public NEWreadListener (NEWIrc i) {
		irc = i;
	}
	public void actionPerformed (ActionEvent e) {
		
		// lock the object in read mode
		irc.sentence.lock_read();
		
		// invoke the method
		String s = irc.sentence.read();
		irc.count.setText(irc.sentence.getNbWritings()+" messages written");
		// unlock the object
		irc.sentence.unlock();
		
		// display the read value
		irc.text.append(s+"\n");
	}
}

class NEWwriteListener implements ActionListener {
	NEWIrc irc;
	public NEWwriteListener (NEWIrc i) {
        	irc = i;
	}
	public void actionPerformed (ActionEvent e) {
		
		// get the value to be written from the buffer
        String s = irc.data.getText();
        	
        	// lock the object in write mode
		irc.sentence.lock_write();
		
		// invoke the method
		irc.sentence.write(NEWIrc.myName+" wrote "+s);
		irc.count.setText(irc.sentence.getNbWritings()+" messages written");
		irc.data.setText("");
		
		// unlock the object
		irc.sentence.unlock();
	}
}



